package com.laikan.reader.core.view;

import android.support.design.widget.Snackbar;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.laikan.library.components.BasicApplication;
import com.laikan.reader.R;

/**
 * Created by Administrator on 2016/9/23.
 */
public class SnackBarNotice {
    private static final int DEFAULT_BACKGROUND_COLOR = BasicApplication.getInstance().getResources().getColor(R.color.C_66ccdd);
    private static final int DEFAULT_TEXT_COLOR = BasicApplication.getInstance().getResources().getColor(R.color.C_FFFFFF);

    public static void customSnackbarShow(View view, int resId) {
        customSnackbarShow(view, resId, -1, -1);
    }

    public static void customSnackbarShow(View view, String msg) {
        customSnackbarShow(view, msg, -1, -1);
    }

    public static void customSnackbarShow(View sourceView, int resId, int messageColor, int backgroundColor) {
        String msg = BasicApplication.getInstance().getResources().getString(resId);
        customSnackbarShow(sourceView, msg, messageColor, backgroundColor);
    }

    public static void customSnackbarShow(View sourceView, String msg, int messageColor, int backgroundColor) {
        if(sourceView == null) return;
        Snackbar snackbar = Snackbar.make(sourceView, "", Snackbar.LENGTH_SHORT);
        //获取snackbar的View(其实就是SnackbarLayout)
        View snackbarview = snackbar.getView();
        if(snackbarview != null){
            //将获取的View转换成
            Snackbar.SnackbarLayout snackbarLayout = (Snackbar.SnackbarLayout)snackbarview;

            View costomRootView = LayoutInflater.from(snackbarview.getContext()).inflate(R.layout.snackbar_content, null);//加载布局文件新建View

            //设置新建布局参数
            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.MATCH_PARENT);
            //设置新建布局在Snackbar内垂直居中显示
            layoutParams.gravity = Gravity.CENTER;
            TextView custom_snackbar_text = (TextView)costomRootView.findViewById(R.id.custom_snackbar_text);
            custom_snackbar_text.setText(msg);

            if(backgroundColor > 0){  //设置View背景色
                snackbarview.setBackgroundColor(backgroundColor);
                costomRootView.setBackgroundColor(backgroundColor);
            }else {
                snackbarview.setBackgroundColor(DEFAULT_BACKGROUND_COLOR);
                costomRootView.setBackgroundColor(DEFAULT_BACKGROUND_COLOR);
            }
            if(messageColor > 0){
                custom_snackbar_text.setTextColor(messageColor);
            }else {
                custom_snackbar_text.setTextColor(DEFAULT_TEXT_COLOR);
            }
            //将新建布局添加进snackbarLayout相应位置
            snackbarLayout.addView(costomRootView, layoutParams);
            snackbar.show();
        }
    }
}
